import { nextTick } from 'vue';
import type { App } from 'vue';
import * as eleSvg from '@element-plus/icons-vue';
import * as aliSvg from '@ant-design/icons-vue';
/**
 * 导出全局注册 Element Plus 图标库
 * @param app vue 实例
 * @description 仅使用图标库，不依赖 Element Plus 组件库
 */
export function elSvg(app: App) {
  const icons = eleSvg as any;
  for (const i in icons) {
    // 确保图标有 name 属性
    if (icons[i] && icons[i].name) {
      app.component(`ele-${icons[i].name}`, icons[i]);
    }
  }
}

/**
 * 导出全局注册 ant design vue svg 图标
 * @param app vue 实例
 * @description 使用：https://antdv.com/components/icon-cn
 */
export function alSvg(app: App) {
  const icons = aliSvg as any;
  for (const i in icons) {
    // 过滤掉不需要的图标
    if (
      icons[i] &&
      icons[i].displayName &&
      !['create', 'Icon4', 'getTwoToneColor', 'setTwoToneColor'].includes(
        icons[i].name
      )
    ) {
      app.component(`ali-${icons[i].displayName}`, icons[i]);
    }
  }
}

// 获取阿里字体图标
export const getAlicdnIconfont = () => {
  return new Promise((resolve, reject) => {
    nextTick(() => {
      const icons = aliSvg as any;
      const sheetsIconList: string[] = [];
      for (const i in icons) {
        if (
          !['create', 'Icon4', 'getTwoToneColor', 'setTwoToneColor'].includes(
            icons[i].name
          )
        ) {
          sheetsIconList.push(`ali-${icons[i].displayName}`);
        }
      }
      if (sheetsIconList.length > 0) resolve(sheetsIconList);
      else reject('未获取到值，请刷新重试');
    });
  });
};

// 初始化获取 Element Plus 图标库，增加了 ele- 前缀，使用时：ele-Aim
export const getElementPlusIconfont = () => {
  return new Promise((resolve, reject) => {
    nextTick(() => {
      const icons = eleSvg as any;
      const sheetsIconList: string[] = [];
      for (const i in icons) {
        sheetsIconList.push(`ele-${icons[i].name}`);
      }
      if (sheetsIconList.length > 0) resolve(sheetsIconList);
      else reject('未获取到值，请刷新重试');
    });
  });
};
